//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SwingExercise.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/BermudanExercise.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CSwingExercise::CSwingExercise (DateTime from, DateTime to, UInt64 stepSizeSecs) : CBermudanExercise(CSwingExercise::typeid)
{
    try
    {
#ifdef HANDLE
        _phSwingExercise = NULL;
#endif
        QuantLib::Date _from = (QuantLib::Date)ValueHelper::Convert (from); //d
        QuantLib::Date _to = (QuantLib::Date)ValueHelper::Convert (to); //d
        QuantLib::Size _stepSizeSecs = (QuantLib::Size)ValueHelper::Convert (stepSizeSecs); //d
        _ppSwingExercise = new boost::shared_ptr<QuantLib::SwingExercise> (new QuantLib::SwingExercise ( _from,  _to,  _stepSizeSecs ));
        SetBermudanExercise (boost::dynamic_pointer_cast<QuantLib::BermudanExercise> (*_ppSwingExercise));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::CSwingExercise::CSwingExercise (Cephei::Core::IVector<DateTime>^ dates, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<UInt64>^>^ seconds) : CBermudanExercise(CSwingExercise::typeid)
{
    try
    {
#ifdef HANDLE
        _phSwingExercise = NULL;
#endif
        dates->Lock();
        INativeVector<DateTime>^ _NCIdates = dates->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCdates = safe_cast<CDateTimeVector^>(_NCIdates);
        std::vector<QuantLib::Date>& _dates = static_cast<std::vector<QuantLib::Date>&> (_NCdates->GetReference ());
        CUInt64Vector^ _NCseconds;
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<UInt64>^>::IsSome::get (seconds))
        {
            seconds->Value->Lock();
            INativeVector<UInt64>^ _NCIseconds = seconds->Value->getFeature (NativeFeature::Value);
            _NCseconds = safe_cast<CUInt64Vector^>(_NCIseconds);
        }
        std::vector<QuantLib::Size>& _seconds = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<UInt64>^>::IsSome::get (seconds) ? static_cast<std::vector<QuantLib::Size>&> (_NCseconds->GetReference ()) : std::vector<QuantLib::Size>()); //3
        _ppSwingExercise = new boost::shared_ptr<QuantLib::SwingExercise> (new QuantLib::SwingExercise ( _dates,  _seconds ));
        SetBermudanExercise (boost::dynamic_pointer_cast<QuantLib::BermudanExercise> (*_ppSwingExercise));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (dates != nullptr) dates->Unlock();    //not optional
        if (seconds != nullptr) seconds->Value->Unlock();
    }
}
Cephei::QL::Instruments::CSwingExercise::CSwingExercise (boost::shared_ptr<QuantLib::SwingExercise>& childNative, Object^ owner) : CBermudanExercise(CSwingExercise::typeid)
{
#ifdef HANDLE
	_phSwingExercise = NULL;
#endif
	_ppSwingExercise = &childNative;
    _ppBermudanExercise = new boost::shared_ptr<QuantLib::BermudanExercise> (boost::dynamic_pointer_cast<QuantLib::BermudanExercise> (*_ppSwingExercise));
}
Cephei::QL::Instruments::CSwingExercise::CSwingExercise (QuantLib::SwingExercise& childNative, Object^ owner) : CBermudanExercise(CSwingExercise::typeid)
{
#ifdef HANDLE
	_phSwingExercise = NULL;
#endif
	_ppSwingExercise = new boost::shared_ptr<QuantLib::SwingExercise> (&childNative);
    _ppBermudanExercise = new boost::shared_ptr<QuantLib::BermudanExercise> (boost::dynamic_pointer_cast<QuantLib::BermudanExercise> (*_ppSwingExercise));
    _SwingExerciseOwner = owner;
    _BermudanExerciseOwner = owner;
}

Cephei::QL::Instruments::CSwingExercise::CSwingExercise (CSwingExercise^ copy) : CBermudanExercise(CSwingExercise::typeid)
{
#ifdef HANDLE
	_phSwingExercise = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSwingExercise = new boost::shared_ptr<QuantLib::SwingExercise> (copy->GetShared());
        _ppBermudanExercise = new boost::shared_ptr<QuantLib::BermudanExercise> (boost::dynamic_pointer_cast<QuantLib::BermudanExercise> (*_ppSwingExercise));
    }
}
Cephei::QL::Instruments::CSwingExercise::CSwingExercise (PLATFORM::Type^ t) : CBermudanExercise(CSwingExercise::typeid)
{
#ifdef HANDLE
	_phSwingExercise = NULL;
#endif
	if (!t->IsSubclassOf(CSwingExercise::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CSwingExercise::CSwingExercise (QuantLib::Handle<QuantLib::SwingExercise>& childNative, Object^ owner)  : CBermudanExercise(CSwingExercise::typeid)
{
	_phSwingExercise = &childNative;
	_ppSwingExercise = &static_cast<boost::shared_ptr<QuantLib::SwingExercise>>(childNative.currentLink());
    _ppBermudanExercise = new boost::shared_ptr<QuantLib::BermudanExercise> (boost::dynamic_pointer_cast<QuantLib::BermudanExercise> (*_ppSwingExercise));
    _SwingExerciseOwner = owner;
}
Cephei::QL::Instruments::CSwingExercise::CSwingExercise (QuantLib::Handle<QuantLib::SwingExercise> childNative)  : CBermudanExercise(CSwingExercise::typeid)
{
	_phSwingExercise = &childNative;
	_ppSwingExercise = &static_cast<boost::shared_ptr<QuantLib::SwingExercise>>(childNative.currentLink());
    _ppBermudanExercise = new boost::shared_ptr<QuantLib::BermudanExercise> (boost::dynamic_pointer_cast<QuantLib::BermudanExercise> (*_ppSwingExercise));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CSwingExercise::CSwingExercise (QuantLib::SwingExercise childNative)  : CBermudanExercise(CSwingExercise::typeid)
{
#ifdef HANDLE
	_phSwingExercise = NULL;
#endif
	_ppSwingExercise = new boost::shared_ptr<QuantLib::SwingExercise> (new QuantLib::SwingExercise (childNative));
    _ppBermudanExercise = new boost::shared_ptr<QuantLib::BermudanExercise> (boost::dynamic_pointer_cast<QuantLib::BermudanExercise> (*_ppSwingExercise));
}
#endif

Cephei::QL::Instruments::CSwingExercise::~CSwingExercise ()
{
    if (_ppSwingExercise != NULL)
    {
	    delete _ppSwingExercise;
        _ppSwingExercise = NULL;
    }
}
Cephei::QL::Instruments::CSwingExercise::!CSwingExercise ()
{
    if (_ppSwingExercise != NULL)
    {
	    delete _ppSwingExercise;
    }
}
QuantLib::SwingExercise& Cephei::QL::Instruments::CSwingExercise::GetReference ()
{
    if (_ppSwingExercise == NULL) throw REFNEW NativeNullException ();
	return **_ppSwingExercise;
}
boost::shared_ptr<QuantLib::SwingExercise>& Cephei::QL::Instruments::CSwingExercise::GetShared ()
{
    if (_ppSwingExercise == NULL) throw REFNEW NativeNullException ();
	return *_ppSwingExercise;
}
QuantLib::SwingExercise* Cephei::QL::Instruments::CSwingExercise::GetPointer ()
{
    if (_ppSwingExercise == NULL) throw REFNEW NativeNullException ();
	return &**_ppSwingExercise;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SwingExercise>& Cephei::QL::Instruments::CSwingExercise::GetHandle ()
{
	if (_phSwingExercise == NULL)
	{
		_phSwingExercise = new Handle<QuantLib::SwingExercise> (*_ppSwingExercise);
	}
	return *_phSwingExercise;
}
#endif
bool Cephei::QL::Instruments::CSwingExercise::HasNative () 
{
	return (_ppSwingExercise != NULL);
}

Cephei::Core::IVector<Double>^ Cephei::QL::Instruments::CSwingExercise::ExerciseTimes (Cephei::QL::Times::IDayCounter^ dc, DateTime refDate)
{
    CDayCounter^ _Cdc;
    try
    {
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        QuantLib::Date _refDate = (QuantLib::Date)ValueHelper::Convert (refDate); //a
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppSwingExercise)->exerciseTimes ( _dc,  _refDate );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::Core::IVector<UInt64>^ Cephei::QL::Instruments::CSwingExercise::Seconds::get ()
{
    try
    {
    	std::vector<QuantLib::Size>& _rv = (std::vector<QuantLib::Size>&)(*_ppSwingExercise)->seconds ( );   
        Cephei::Core::IVector<UInt64>^ _nrv = REFNEW CoVector<UInt64> (REFNEW CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::ISwingExercise^ Cephei::QL::Instruments::CSwingExercise_Factory::Create (DateTime from, DateTime to, UInt64 stepSizeSecs)
{
    return REFNEW CSwingExercise ( from,  to,  stepSizeSecs);
}
Cephei::QL::Instruments::ISwingExercise^ Cephei::QL::Instruments::CSwingExercise_Factory::Create (Cephei::Core::IVector<DateTime>^ dates, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<UInt64>^>^ seconds)
{
    return REFNEW CSwingExercise ( dates,  seconds);
}
